/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "ITexture.h"
#include "IRenderer.h"
#include "core/core.h"

#ifdef CORE_DIRECTX
#include "dx/Texture.h"
#endif

ITexture** ITexture::s_ppCurrentTextures = NULL;

ITexture::ITexture()
:	m_bIsLoaded(false),
	m_bIsRenderTarget(false),
	m_bIsCubeMap(false),
	m_bIsVolTexture(false),
	m_iWidth(0),
	m_iHeight(0),
	m_iDepth(1),
	m_pRawData(NULL),
	m_iRawDataStride(0),
	m_iCurrentTarget(-1),
	m_pScreenQuad(NULL),
	m_bHasDepthBuffer(false)
{
	if (s_ppCurrentTextures == NULL) {
		s_ppCurrentTextures = (ITexture**) malloc(NUMTEXTURES * sizeof(ITexture*));
		memset(s_ppCurrentTextures, 0, NUMTEXTURES * sizeof(ITexture*));
	}
	pRenderer = coreGetRenderer();
}

ITexture::~ITexture(void)
{
	DELETE_ARRAY(s_ppCurrentTextures);
	DELETE_POINTER(m_pRawData);
	DELETE_POINTER(m_pScreenQuad);
}

ITexture* ITexture::CreateRenderTarget(int width, int height, eFormat format, bool useDepth)
{
	ITexture *res = NULL;

#ifdef CORE_DIRECTX
	res = new directx::Texture;
	dynamic_cast<directx::Texture*>(res)->createRenderTarget(width, height, format, useDepth);
#endif

	return res;
}

ITexture* ITexture::CreateRenderToTexture(int width, int height, eFormat format, bool usedepth)
{
	ITexture *res = NULL;

#ifdef CORE_DIRECTX
	res = new directx::Texture;
	dynamic_cast<directx::Texture*>(res)->createRenderToTexture(width, height, format, usedepth);
#endif

	return res;
}

ITexture* ITexture::CreateFromData(int width, int height, eFormat format, char* data)
{
	ITexture *res = NULL;

#ifdef CORE_DIRECTX
	res = new directx::Texture;
	dynamic_cast<directx::Texture*>(res)->createFromData(width, height, format, data);
#endif

	return res;
}

const int ITexture::getWidth() const
{
	return m_iWidth;
}

const int ITexture::getHeight() const
{
	return m_iHeight;
}

const int ITexture::getDepth() const
{
	return m_iDepth;
}

const bool ITexture::isRenderTarget() const
{
	return m_bIsRenderTarget;
}

const bool ITexture::isCubeMap() const
{
	return m_bIsCubeMap;
}

const bool ITexture::isVolumeTexture() const
{
	return m_bIsVolTexture;
}

const bool ITexture::isLoaded() const
{
	return m_bIsLoaded;
}

const eFormat ITexture::getFormat() const
{
	return m_eFormat;
}

std::string ITexture::getFilename() const
{
	return sFilename;
}

const math::Vector2i ITexture::getSize() const
{
	return math::Vector2i(m_iWidth, m_iHeight);
}

const bool ITexture::hasDepthBuffer() const
{
	return m_bHasDepthBuffer;
}